
<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
         <el-form-item label="角色名称" prop="roleName">
            <el-input
               v-model="queryParams.roleName"
               placeholder="请输入角色名称"
               clearable
               style="width: 240px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="权限标识" prop="roleKey">
            <el-input
               v-model="queryParams.roleKey"
               placeholder="请输入权限标识"
               clearable
               style="width: 240px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="openSelectRole"
               v-hasPermi="['system:allot:menu2role']"
            >添加角色</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="CircleClose"
               :disabled="multiple"
               @click="cancelAuthRole"
               v-hasPermi="['system:allot:menu2role']"
            >批量取消授权</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Close"
               @click="handleClose"
            >关闭</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="角色编号" prop="id" :show-overflow-tooltip="true" />
         <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" />
         <el-table-column label="权限标识" prop="roleKey" :show-overflow-tooltip="true" />
        <el-table-column label="数据权限" align="center" prop="dataScope" :show-overflow-tooltip="true">
          <template #default="scope">
            <dict-tag :options="DataScopeType" :value="scope.row.dataScope" />
          </template>
        </el-table-column>
         <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
               <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
            </template>
         </el-table-column>
         <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="CircleClose" @click="cancelAuthRole(scope.row)" v-hasPermi="['system:allot:menu2role']">取消授权</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
      <select-role ref="selectRef" :menuId="queryParams.menuId" @ok="handleQuery" />
   </div>
</template>

<script setup name="AuthRole">
import { allotRoleList, allotRoleAdd, allotRoleDelete } from "@/api/system/menu";
import selectRole from "./selectRole";


const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const { DataScopeType } = proxy.useEDict("DataScopeType");

const roleList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const roleIds = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  menuId: route.params.menuId,
  roleName: undefined,
  roleKey: undefined,
  matchMenuId: 1
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  allotRoleList(queryParams).then(response => {
    roleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/system/menu" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  roleIds.value = selection.map(item => item.id);
  multiple.value = !selection.length;
}
/** 打开授权用户表弹窗 */
function openSelectRole() {
  proxy.$refs["selectRef"].show();
}
/** 取消授权按钮操作 */
function cancelAuthRole(row) {
    let ids = row.id || roleIds.value;
    if(!Array.isArray(ids)) ids=[ids];
    proxy.$modal.confirm('确认要取消角色编号为"' + ids + '"的菜单吗？').then(function () {
    return allotRoleDelete({ roleIds: ids, menuId: queryParams.menuId });
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("取消授权成功");
  }).catch(() => {});
}

getList();
</script>
